package com.core.controller;

import com.core.annotation.OperationLog;
import com.core.domain.Permissions;
import com.core.resp.AjaxResponse;
import com.core.resp.PageResult;
import com.core.service.PermissionsService;
import com.core.vo.PermissionVo;
import com.core.vo.UserRolesVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : HUANG
 * @create : 2025/6/1 10:42
 * @function :
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    private static final Logger log = LoggerFactory.getLogger(PermissionController.class);
    @Autowired
    private PermissionsService permissionsService;

    @PreAuthorize("@ss.hasAuthority('permission:read')")
    @GetMapping("/get")
    @OperationLog(type = "list", module = "permission", description = "获取权限列表")
    public AjaxResponse getPermissions() {
        List<Permissions> data = permissionsService.list();
        return AjaxResponse.success(200, "查询成功", data);
    }

    @PutMapping("/put")
    public AjaxResponse putPermission(@RequestBody UserRolesVo permissionVo) {
//        try {
//            usersService.updateUserPermissions(userRolesVo.getUserId(), userRolesVo.getPermissionIds());
//            return AjaxResponse.success(200, "用户权限更新成功", null);
//        } catch (Exception e) {
//            log.error("Error updating user permissions: " + e.getMessage());
//            return AjaxResponse.error(500, "用户权限更新失败");
//        }
        log.info(permissionVo.toString());
        return null;
    }


    @PreAuthorize("@ss.hasAuthority('permission:list')")
    @PostMapping("/get/page/{pageSize}/{pageNum}")
    @OperationLog(type = "list", module = "permission", description = "分页获取权限列表")
    public AjaxResponse getPermissionPage(@PathVariable Long pageSize, @PathVariable Long pageNum, @RequestBody PermissionVo permissionVo) {
        PageResult<Permissions> permissionsPageResult = permissionsService.selectPage(pageSize, pageNum, permissionVo == null ? null : permissionVo.getDescription());
        return AjaxResponse.success(200, "查询成功", permissionsPageResult);
    }

    @PreAuthorize("@ss.hasAuthority('permission:create')")
    @PostMapping("/add")
    @OperationLog(type = "create", module = "permission", description = "添加权限: #permission.description")
    public AjaxResponse addPermission(@RequestBody Permissions permissions) {
        try {
            AjaxResponse ajaxResponse = permissionsService.addPermission(permissions);
            return ajaxResponse;
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResponse.error(405, "添加失败");
        }
    }
    @PreAuthorize("@ss.hasAuthority('permission:delete')")
    @OperationLog(type = "delete", module = "permission", description = "删除权限: #permission.description")
    @PostMapping("/batchDelete")
    public AjaxResponse deletePermission(@RequestBody List<Long> ids) {
        try {
            return permissionsService.deletePermisson(ids);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    @PreAuthorize("@ss.hasAuthority('permission:update')")
    @PutMapping("/update")
    @OperationLog(type = "update", module = "permission", description = "更新权限, 权限ID: #permission.id, 描述: #permission.description")
    public AjaxResponse updatePermission(@RequestBody PermissionVo permissionVo) {
        try {
            permissionsService.updateById(permissionVo);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResponse.error(405, "更新失败");
        }
        return AjaxResponse.success(200, "更新成功", null);
    }
}
